<template>
    <div class="user-edit all-height" v-loading="loading">
        <!-- Drawer抽屉 -->
        <div :style="{ maxHeight: maxHeight }" class="drawer-content">
            <div :style="{ maxHeight: maxHeight }" class="drawer-content all-height">
                <div class="drawer-header">
                    <div style="margin-right: 16px" v-show="type == '2'">添加区</div>
                    <div style="margin-right: 16px" v-show="type != '2'">编辑区</div>
                    <!-- <Avatar :src="form.avatar" size="large" v-show="type != '2'"/> -->
                </div>
                <Form ref="form" :model="form" :rules="formValidate" label-position="left" :label-width="85" class="marginForm">
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="操作人" prop="person">
                                <Input v-model="form.person" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="操作时间" prop="time">
                                <DatePicker type="date" v-model="form.time" clearable transfer style="width: 100%"></DatePicker>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="bysId" prop="bysId">
                                <Input v-model="form.bysId" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="xjId" prop="xjId">
                                <Input v-model="form.xjId" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="idCard" prop="idCard">
                                <Input v-model="form.idCard" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="是否有异动数据" prop="sfyyd">
                                <Input v-model="form.sfyyd" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </div>
            <div class="drawer-footer br" v-show="type != '0'">
                <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
                <Button @click="visible = false">取消</Button>
            </div>
        </div>
    </div>
</template>

<script>
// 根据你的实际请求api.js位置路径修改
import { addBysXjCompare, editBysXjCompare } from '@/api/bys/xj/xjCompare.js';
export default {
    name: 'addEdit',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false
        },
        data: {
            type: Object
        },
        type: {
            type: String,
            default: '0'
        }
    },
    data() {
        return {
            loading: false,
            title: '',
            submitLoading: false,
            maxHeight: 510,
            form: {
                // 添加或编辑表单对象初始化数据
                person: '',
                time: '',
                bysId: '',
                xjId: '',
                idCard: '',
                sfyyd: ''
            },
            // 表单验证规则
            formValidate: {}
        };
    },
    methods: {
        init() {},
        submit() {
            this.loading = true;
            this.$refs.form.validate(valid => {
                if (valid) {
                    if (typeof this.form.time == 'object') {
                        this.form.time = this.format(this.form.time, 'yyyy-MM-dd');
                    }
                    if (this.type == '1') {
                        // 编辑
                        this.submitLoading = true;
                        editBysXjCompare(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.loading = false;
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    } else {
                        // 添加
                        this.submitLoading = true;
                        addBysXjCompare(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    }
                }
            });
        },
        setCurrentValue(value) {
            if (value === this.visible) {
                return;
            }
            if (this.type == '1') {
                this.title = '编辑';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else if (this.type == '2') {
                this.title = '添加';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else {
                this.title = '详细信息';
                this.maxHeight = '100%';
            }
            // 清空数据
            this.$refs.form.resetFields();
            if (this.type == '0' || this.type == '1') {
                // 回显数据处理
                this.form = this.data;
            } else {
                // 添加
                delete this.form.id;
            }
            this.visible = value;
        }
    },
    watch: {
        value(val) {
            this.setCurrentValue(val);
        },
        visible(value) {
            this.$emit('input', value);
        }
    },
    mounted() {
        this.init();
    }
};
</script>

<style lang="less">
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
// @import "../../../styles/drawer-common.less";
.drawer-footer {
    z-index: 10;
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: left;
    background: #fff;
}

.drawer-content {
    overflow: auto;
}

.drawer-content::-webkit-scrollbar {
    display: none;
}

.drawer-header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
}

.drawer-title {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    display: block;
    margin-bottom: 16px;
}
</style>
